package com.applet.applet_shangcheng.service;

import com.applet.applet_shangcheng.dao.IntegralGoodsMapper;
import com.applet.applet_shangcheng.dao.IntegralOrderMapper;
import com.applet.applet_shangcheng.dao.UserMapper;
import com.applet.applet_shangcheng.entity.IntegralGoods;
import com.applet.applet_shangcheng.entity.IntegralOrder;
import com.applet.applet_shangcheng.entity.Order;
import com.applet.applet_shangcheng.utlis.IdWorker;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class IntegralOrderService {
    @Autowired
    private IntegralOrderMapper integralOrderMapper;

    @Autowired
    private IntegralGoodsMapper integralGoodsMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IdWorker idWorker;

    public int updateOrder(IntegralOrder integralOrder) {
        int result = integralOrderMapper.updateOrder(integralOrder);
        return result;
    }

    ;

    public List<IntegralOrder> findAll(String username) {
        List<IntegralOrder> orderList = integralOrderMapper.findAll(username);
        return orderList;
    }

    ;

    public Map findById(String orderId) {
        Map<String,Object> map = new HashMap<>();
        IntegralOrder order = integralOrderMapper.findById(orderId);
        String name = order.getOrderName();
        IntegralGoods goods = integralGoodsMapper.findByGoodsName(name);
        map.put("order",order);
        map.put("goods",goods);
        return map;
    }

    public int deleteOrder(int id) {
        int result = integralOrderMapper.deleteOrder(id);
        return result;
    }

    ;

    /*
    goodsId:商品id
    sum_number:商品总个数
`   username'用户名称',
    consignee` '收货人',
`   phone'收货人手机号',
  `consignee_address收货地址',
   */
    @Transactional(rollbackFor = Exception.class)
    public Map<String,Object> exchangeOrder(IntegralOrder integralOrder) {
        //IntegralGoods goods = integralGoodsMapper.findByGoodsId(goodsId);
       Map<String,Object> map= new HashMap<>();
        integralOrder.setOrderId(String.valueOf(idWorker.nextId()));
        IntegralGoods goods = integralGoodsMapper.findByGoodsName(integralOrder.getOrderName());
        int SumNumber = goods.getIntegral()*integralOrder.getSumNumber();
        integralOrder.setSumPrice(SumNumber);
        integralOrder.setCreateTime(new Date());
        integralOrder.setPayTime(new Date());
        integralOrder.setOrderStatus(0);
        integralOrder.setEmissionStatus(0);
        integralOrder.setOrderStatus(0);
        integralOrder.setPayMode(0);
        int result = integralOrderMapper.insertOrder(integralOrder);
        map.put("goods",goods);
        map.put("integralOrder",integralOrder);
        return map;
    }

    public PageResult findAlls(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<IntegralOrder> page = integralOrderMapper.findAlls(queryString);
        //PageInfo<Announcement> pageInfo = new PageInfo<>(announcementList);
        return new PageResult(page.getTotal(),page.getResult() );
    }

    public IntegralOrder findByIdOrder(String id) {
        return integralOrderMapper.integralOrder(id);
    }

    public int updateOrders(IntegralOrder integralOrder) {
        return integralOrderMapper.updateOrder1(integralOrder);
    }

    public List<Map<String, String>> getGoodsClass() {
        return integralOrderMapper.getGoodsClass();
    }
}
